<?php
/**
 * @file $HeadURL: libraryindexcontroller.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-20 3:53:12 $
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\library;

use haibao\frame\data\query\Condition;
use \haibao\cms\data\imagecollective\ImageCollective as ImageCollectiveData;
use \haibao\cms\business\control\ArticleController as ArticleController;
use \haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollectiveFilter;
use \haibao\cms\business\Brand as BrandBusiness;


class LibraryIndexController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
	    $this->savaLog();
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$newFeature = $featureSerial = $brandInfoArr = $starInfoArr = $newProduct = $designerTrends = $designerInfoArr = $brandAreaAndPosition = $starAreaAndPosition = array();
		
		if($controlData['manual'] && ($controlData['manual']['contorllerType'] == 'starExample')){
		    //取最新6个专题
		    $newFeature = \haibao\cms\business\control\FeatureController::getFeatureGroupBySerialId();
		    //formatDate
		    foreach($newFeature as $key=>$val){
		        $newFeature[$key]['ListTime'] = $this->formatDate($val['ListTime']);
		        $serialInfo = \haibao\cms\business\control\FeatureController::getSerialInfo($val['SerialId']);
		        if($serialInfo){
		            $newFeature[$key]['SerialLogoUrl'] = $serialInfo[$val['SerialId']]['SerialLogoUrlLibr'];
		        }else{
		            $newFeature[$key]['SerialLogoUrl'] = '';
		        }
		    }
		    $this->assign('newFeature', $newFeature);
		    //独家策划
		    //$featureSerial = \haibao\cms\business\control\FeatureController::$featureSerial;
		    $featureLogo = \haibao\cms\business\control\FeatureController::$featureLogo; 
		    $featureSerial = \haibao\cms\business\control\FeatureController::getFeatureSerialArray();
		    $this->assign('featureSerial', $featureSerial);
		    $this->assign('featureLogo', $featureLogo);
		}

        
        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'Library'){
            //品牌推荐
            $brandIdArr = \haibao\cms\business\Brand::getLibraryBrandRecommend();
            if($brandIdArr){
                $brandInfo = \haibao\cms\business\Brand::getBrandInfoByTagIds($brandIdArr);
                foreach ($brandInfo as $bdkey => $bdvalue){
                    $brandInfoArr[$bdkey]['Id'] = $bdvalue->Id;
                    $brandInfoArr[$bdkey]['isIncludeCn'] = $this->getIncludeCn(trim($bdvalue->NameEN));
                    $brandInfoArr[$bdkey]['isRepeatName'] = $this->getRepeatName(trim($bdvalue->NameCN),trim($bdvalue->NameEN));
                    $brandInfoArr[$bdkey]['BrandUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/brand/'.$bdvalue->BrandUrl.'/';
                    $temp = \haibao\cms\business\ImageLibrary::getImageUrlById($bdvalue->ImgId);
                    $brandInfoArr[$bdkey]['ImageUrl'] = isset($temp[$bdvalue->ImgId]) ? $temp[$bdvalue->ImgId] : '';//isset($tempUrl['tempUrl'][$bdvalue->ImgId]) ? self::getImageLink($tempUrl['tempUrl'][$bdvalue->ImgId],160,160) : '';
                    $brandInfoArr[$bdkey]['NameCN'] = $bdvalue->NameCN;
                    $brandInfoArr[$bdkey]['NameEN'] = $bdvalue->NameEN;
                    $brandInfoArr[$bdkey]['CurDesigner'] = $bdvalue->CurDesigner;
                }
            }
            $brandAreaAndPosition = $this->getAreaAndPosition(1);//品牌所在地区，明星分类
            
            //人气明星
            $starIdArr = \haibao\cms\business\Celebrity::getLibraryStarRecommend();
            if($starIdArr){
                $starInfo = \haibao\cms\business\Celebrity::getCelebrityProInfoByTagIds($starIdArr);
                $starImage = array();
                foreach($starInfo as $starkey=>$starvalue){
                    $starInfoArr[$starkey]['Id'] = $starvalue->Id;
                    $starInfoArr[$starkey]['NameCN'] = $starvalue->NameCn;
                    $starInfoArr[$starkey]['NameEN'] = $starvalue->NameEn;
                    $starInfoArr[$starkey]['Birthday'] = $starvalue->Birthday;
                    $temp = \haibao\cms\business\ImageLibrary::getImageUrlById($starvalue->CelebrityImage->ImageId);
                    $starInfoArr[$starkey]['ImageUrl'] = isset($temp[$starvalue->CelebrityImage->ImageId]) ? $temp[$starvalue->CelebrityImage->ImageId] : '';//isset($tempUrl['tempUrl'][$starvalue->CelebrityImage->ImageId]) ? self::getImageLink($tempUrl['tempUrl'][$starvalue->CelebrityImage->ImageId],100,100) : '';
                    $starInfoArr[$starkey]['Birthday'] = $starvalue->Birthday;
                    $starInfoArr[$starkey]['StarUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/star/'.$starvalue->CelebrityUrl.'/';
                    $starInfoArr[$starkey]['EditFansNum'] = $starvalue->CelebrityRiseCount->EditFansNum;
                    $starInfoArr[$starkey]['Xingzuo'] = $starvalue->CelebrityInfo->XingZuo;
                }
            }
          
            $starAreaAndPosition = $this->getAreaAndPosition(2);//品牌所在地区，明星分类
            $this->assign('starPosition', $starAreaAndPosition);
            $this->assign('brandPosition', $brandAreaAndPosition);
        }
        $this->assign('brandInfoArr', $brandInfoArr);
        $this->assign('starInfoArr', $starInfoArr);
        
        
        //新品上架
        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'newProduct'){
            $newProduct = $this->getNewProduct($controlData['manual']['limit']);	//limit 改为16
            if($newProduct){
                foreach($newProduct as $key =>$value){
                    $newProduct[$key]['ProductUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/product/'.$value['oldProductId'].'/';
                    	
                    if(isset($value['BrandUrl'])){
                        $newProduct[$key]['BrandUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).$value['BrandUrl'];
                    }else{
                        $newProduct[$key]['BrandUrl'] = "";
                    }
                }
            }
           
        }
        $this->assign('newProduct', $newProduct);
        
		
		//设计师动态
		$designerController = new \haibao\cms\business\control\pc\channel\design\DesignIndexController();
		$designerTrends = $designerController->getDesignDynamic();
		$designerIdArr = \haibao\cms\business\Designer::getLibraryDesignerRecommend();
		if($designerIdArr){
		    $designerInfo = \haibao\cms\business\Designer::getDesignerInfoByTagIds($designerIdArr);
		    foreach($designerInfo as $desikey=>$desivalue){
		        $designerInfoArr[$desikey]['NameCN'] = $desivalue->NameCN;
		        $designerInfoArr[$desikey]['NameEN'] = $desivalue->NameEN;
		        $designerInfoArr[$desikey]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_DESIGNER).'/designer/'.$desivalue->TopicId.'/';
		    }
		}
		
		$this->assign('designerTrends', $designerTrends);
		$this->assign('designerInfoArr', $designerInfoArr);
		
		//活跃明星文章
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'Brand'){
		    if($controlData['manual']['limit']){
		        $param['limit'] = $controlData['manual']['limit'];
		        $param['relType']=2;
		        $param['tagType']=4;
		        $param['proImageLevel'] = 5;
		        $starArticleData = $this->getStarOrBrandArticle($param);
		        if($starArticleData){
		            foreach($starArticleData as $key =>$value){
		                $starArticleData[$key]['ArticleUrl'] = $this->getArticleUrl($value['ArticleInfo'][0]['PublishId'],$value['ArticleInfo'][0]['TagId']);
		                $starArticleData[$key]['TopicUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$value['Url'];
		                $starArticleData[$key]['ProImage'] = $value['ArticleInfo'][0]['ProImage'];
		                $starArticleData[$key]['Title'] = $value['ArticleInfo'][0]['Title'];
		                $starArticleData[$key]['ShortTitle'] = $value['ArticleInfo'][0]['ShortTitle'];
		                $starArticleData[$key]['ArticleId'] = $value['ArticleInfo'][0]['ArticleId'];
		            }
		        }
		
		        $starAreaAndPosition = $this->getAreaAndPosition($param['tagType']);//明星所在地区，明星分类
		
		        //品牌推荐文章
		        $param['relType']=1;
		        $param['tagType']=1;
		        $param['proImageLevel'] = 4;
		        $brandArticleData = $this->getStarOrBrandArticle($param);
		        foreach($brandArticleData as $key =>$value){
		            /* 是否包含汉字 是否重名 */
		            $brandArticleData[$key]['isIncludeCn'] = $this->getIncludeCn(trim($brandArticleData[$key]['NameEn']));
		            $brandArticleData[$key]['isRepeatName'] = $this->getRepeatName(trim($brandArticleData[$key]['NameCn']),trim($brandArticleData[$key]['NameEn']));
		             
		            $brandArticleData[$key]['ArticleUrl'] = $this->getArticleUrl($value['ArticleInfo'][0]['PublishId'],$value['ArticleInfo'][0]['ParentId']);
		            // 					$brandArticleData[$key]['ArticleUrl'] = $this->getArticleUrl($value['ArticleInfo'][0]['PublishId'],$value['ArticleInfo'][0]['TagId']);
		            $brandArticleData[$key]['ProImage'] = $value['ArticleInfo'][0]['ProImage'];
		            $brandArticleData[$key]['Title'] = $value['ArticleInfo'][0]['Title'];
		            $brandArticleData[$key]['ShortTitle'] = $value['ArticleInfo'][0]['ShortTitle'];
		            $brandArticleData[$key]['ArticleId'] = $value['ArticleInfo'][0]['ArticleId'];
		            if(isset($value['Url'])){
		                $brandArticleData[$key]['TopicUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).$value['Url'];
		            }else{
		                $brandArticleData[$key]['TopicUrl'] = '';
		            }
		        }
		        $brandAreaAndPosition = $this->getAreaAndPosition($param['tagType']);//品牌所在地区，明星分类
		         
		        $key = 0;
		        for($i=ord('A');$i<=ord('Z');$i++){
		            $searchArr[$key] = chr($i);
		            $key++;
		        }
		        array_push($searchArr, "其他");
		    }
		    $isHomePage = 2;
		    $this->assign('brandsHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS));
		    $this->assign('searchArr', $searchArr);
		    $this->assign('starPosition', $starAreaAndPosition);
		    $this->assign('brandPosition', $brandAreaAndPosition);
		    $this->assign('starArticleData', $starArticleData);
		    $this->assign('brandArticleData', $brandArticleData);
		    $this->assign('brandArticleDataCount', count($brandArticleData));
		    $this->assign('isHomePage', $isHomePage);
		}
		

		
		
		$this->assign('BrandsUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS));
		$this->assign('StarsUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS));
		$this->assign('DesignersUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_DESIGNER));
		$this->assign('ProductUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/product/list_0_0/');
		$this->assign('ExclusiveUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
		$this->assign('PicUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
		$this->assign('markType', ArticleController::INFO_TYPE_ARTICLE);
		$this->assign('attentionTypeStar', \haibao\www\model\data\Favorite::ATTENTION_TYPE_STAR);
		$this->assign('attentionTypeBrand', \haibao\www\model\data\Favorite::ATTENTION_TYPE_BRAND);
		$isNewHtml = $this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	
	
	
	private function getNewProduct($limit){
		$articleControl = ArticleController::initArticleControl();
		return $articleControl->getNewProductAndBrand($limit);
	}
	
	private function getStarOrBrandArticle($param){
	    $data = array();
	    $result= array();
	    $articleControl = ArticleController::initArticleControl();
	    $queryCondition['relType'] = $param['relType'];//1为品牌，2为明星
	    $queryCondition['limit'] = $param['limit'];
	    $queryCondition['isProImage'] = 1; //是否有导读图
	    $queryCondition['proImageLevel'] = $param['proImageLevel'];//为400*400
	    $queryCondition['source'] = __CLASS__;
	    $queryCondition['isTag'] = 1;//是否有标签
	    $queryCondition['tagType'] = $param['tagType'];//0为normal,1为品牌,2为产品,3为组合,4为明星
	    $this->getArticleControl(3,$queryCondition);
	    $result = $articleControl->getChannelArticleToBrandOrStar();
	
	    //为了前台样式能确定第几个关系数组
	    foreach($result as $key => $value){
	        $data[] = $result[$key];
	    }
	    return $data;
	}
	
	
	
	/**
	 * 
	 * @param int $type
	 * 类型 1:品牌, 2:明星 
	 */
	private function getAreaAndPosition($type=1){
		$data=array();
		$brandBusiness = new BrandBusiness();
		if($type==1){
			$data['Region'] = \haibao\cms\business\Brand::$brandProperty;
			$data['Place'] = \haibao\cms\business\Brand::$brandRegion;
		}else{
			$data['Region'] = \haibao\cms\business\Celebrity::$celebrityCategory;
			$data['Place'] = \haibao\cms\business\Celebrity::$celebrityArea;
		}	
		return $data;
	}
	
	
}